package com.bwie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.ProductImgMapper;
import com.bwie.mapper.ProductMapper;
import com.bwie.pojo.Product;
import com.bwie.pojo.ProductImg;
import com.bwie.pojo.ProductType;
import com.bwie.service.ProductService;
import com.bwie.utils.R;
import com.bwie.vo.ProductVo;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    ProductImgMapper productImgMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedissonClient redissonClient;
    @Override
    public List<Product> productList() {

        return productMapper.productList();
    }

    /**
     * @description: 商品添加发布
     * @author: xushuaihang
     * @Date: 2024/6/17 10:24
     * @param:
     * @param productVo
     * @return: com.bwie.utils.R
    **/
    @Override
    public R addProduct(ProductVo productVo) {

        RBloomFilter<Object> bloomProduct = redissonClient.getBloomFilter("bloomProduct");

        bloomProduct.tryInit(10000000,0.03);

        if(bloomProduct.contains(productVo.getProductName())){
            return R.error().message("标题已经存在");
        }

        Product product = BeanUtil.toBean(productVo, Product.class);

        product.setCreateTime(new Date());

        productMapper.insert(product);

        for (ProductImg productImg : productVo.imgList) {

            productImg.setProductId(product.getId());

            productImgMapper.insert(productImg);

        }

        ListOperations<String, String> operations = stringRedisTemplate.opsForList();

        List<Product> list = productMapper.productList();

        operations.set("product:list",0, JSON.toJSONString(list));


        return R.ok().message("添加成功！");
    }

    @Override
    public List<ProductType> productTypeList() {

        return productMapper.productTypeList();
    }

    /**
     * @description: 定时将标题存入布隆
     * @author: xushuaihang
     * @Date: 2024/6/17 11:35
     * @param:
     * @return: void
     **/
    @Scheduled(cron = "0 0/3 * * * ?")
    public void  timingProduct(){
        RBloomFilter<Object> bloomProduct = redissonClient.getBloomFilter("bloomProduct");

        bloomProduct.tryInit(10000000,0.03);

        List<Product> list = productMapper.selectList(new QueryWrapper<>());

        for (Product product : list) {

            if(!bloomProduct.contains(product.getProductName())){
                bloomProduct.add(product.getProductName());
            }

        }
    }

}
